<?php

error_reporting ( E_ALL ^ E_NOTICE );

class parser {
	private $xml, $xml2array;
	
	/**
	 * Constructor
	 *
	 * @param (string) Filename of xml fields
	 */
	public function __construct($fields = false) {
		$this->xml = file_get_contents ( ($fields && file_exists ( $fields )) ? $fields : $GLOBALS ['xmlFormFieldsFile'] ) or die ();
	}
	
	/**
	 * Internal function for xml2array transmitting
	 */
	private function _parseChild($values, &$i) {
		$child = array ();
		if (isset ( $values [$i] ['value'] ))
			array_push ( $child, $values [$i] ['value'] );
		
		while ( $i ++ < count ( $values ) ) {
			switch ($values [$i] ['type']) {
				case 'cdata' :
					array_push ( $child, $values [$i] ['value'] );
					break;
				
				case 'complete' :
					$name = $values [$i] ['tag'];
					if (! empty ( $name )) {
						$child [$name] = ($values [$i] ['value']) ? trim ( $values [$i] ['value'] ) : '';
						if (isset ( $values [$i] ['attributes'] )) {
							$child [$name] = $values [$i] ['attributes'];
						}
					}
					break;
				
				case 'open' :
					$name = $values [$i] ['tag'];
					$size = isset ( $child [$name] ) ? sizeof ( $child [$name] ) : 0;
					$child [$name] [$size] = $this->_parseChild ( $values, $i );
					break;
				
				case 'close' :
					return $child;
					break;
			}
		}
		return $child;
	}
	
	/**
	 * Returns array of xml nodes
	 *
	 * @return array
	 */
	public function xml2array() {
		$xml = & $this->xml;
		$values = $index = $array = array ();
		$parser = xml_parser_create ();
		xml_parser_set_option ( $parser, XML_OPTION_SKIP_WHITE, 1 );
		xml_parser_set_option ( $parser, XML_OPTION_CASE_FOLDING, 0 );
		xml_parse_into_struct ( $parser, $xml, $values, $index );
		xml_parser_free ( $parser );
		$i = 0;
		
		$name = $values [$i] ['tag'];
		$array [$name] = isset ( $values [$i] ['attributes'] ) ? $values [$i] ['attributes'] : '';
		$array [$name] = $this->_parseChild ( $values, $i );
		
		$this->xml2array = $array;
		//die( print_r( $this->xml2array, 1 ));
		

		return $array;
		/*print_r( new SimpleXMLElement($xml) );
		die;*/
	}
	
	/**
	 * Returns fields of form for use in html building
	 *
	 * @param (array) Default values for fields (keys = names)
	 * @return array( 'caption', 'html' )
	 */
	public function getHtmlFields($values = array(), $wap = 0, $namesAsArrays = 0 ) {
		$r = ($this->xml2array) ? $this->xml2array : $this->xml2array ();
		
		$out = array ();
		if (count ( $r ['form'] )) {
			$r = $r ['form'];
			
			if (count ( $r ['field'] )) {
				foreach ( $r ['field'] as $field ) {
					$temp ['caption'] = $wap
						? iconv ( 'utf-8', 'windows-1251', $field ['caption'] ) . ($field ['required'] ? ' <span class="z">*</span>' : '')
						: ($field ['required'] ? '<span style="color:red">*</span> ' : '') . '<b>' . iconv ( 'utf-8', 'windows-1251', $field ['caption'] ) . '</b>';
					
					switch ($field ['type']) {
						case 'textarea' :
							{
								$temp ['html'] = '<textarea name="' . $field ['name'] . ( $namesAsArrays ? '[]' : '' ) . '" rows="' . ($field ['rows'] ? $wap ? 2 : $field ['rows'] : 8) . '" style="' . ($field ['style'] ? $field ['style'] : '') . '">' . (($t = $values [$field ['name']]) ? $t : '') . '</textarea>';
							}
							break;
						
						case 'select' :
							{
								$options = array( );
								foreach( explode( "\n", iconv ( 'utf-8', 'windows-1251', $field['options'] ) ) as $option ) {
									list( $optValue, $optText ) = explode( '||', $option );
									$options[] = '<option value="' . trim( $optValue ) . '">' . trim( $optText ) . '</option>';
								}
								$temp ['html'] = '<select name="' . $field ['name'] . '"' . ($field ['style'] ? ' style="' . $field ['style'] . '"' : '') . '>' . join( "\n", $options ) . '</select>';
							}
							break;

							
						case 'checkbox' :
							{
								$temp ['html'] = '<input type="checkbox" name="' . $field ['name'] . ( $namesAsArrays ? '[]' : '' ) . '"' . (($t = $values [$field ['name']]) ? (' value="' . $t . '"') : '') . ($field ['style'] ? ' style="' . $field ['style'] . '"' : '') . ($field['checked'] ? ' checked="yes"' : '') . '/>';
							}
							break;
							
						case 'file' :
							{
								$temp ['html'] = '<input type="file" name="' . $field ['name'] . ( $namesAsArrays ? '[]' : '' ) . '"' . ($field ['style'] ? ' style="' . $field ['style'] : '') . '/>';
							}
							break;
							
						case 'input' :
						default :
							{
								$temp ['html'] = '<input name="' . $field ['name'] . ( $namesAsArrays ? '[]' : '' ) . '" ' . (($t = $values [$field ['name']]) ? ('value="' . $t . '" ') : '') . 'maxlength="' . ($field ['maxlength'] ? $field ['maxlength'] : 255) . '" style="' . ($field ['style'] ? $field ['style'] : '') . '"/>';
							}
					}
					$out [] = $temp;
					$temp = array ();
				}
			}
		}
		return $out;
	}
	
	/**
	 * Returns true|false for enable|disable video adding
	 *
	 * @return bool
	 */
	public function videoEnabled() {
		($this->xml2array) ? null : $this->xml2array ();
		return ( bool ) $this->xml2array ['form'] ['video'] ['enabled'];
	}
	
	/**
	 * Returns number of photos
	 *
	 * @return int
	 */
	public function photosNumber() {
		($this->xml2array) ? null : $this->xml2array ();
		return ( int ) $this->xml2array ['form'] ['photos'] ['number'];
	}
	
	/**
	 * Returns fields of form for use in mysql queries or input checking
	 *
	 * @param (bool) Return fields of short msg?
	 * @return array( 'name', 'caption', (bool) required? )
	 */
	public function getFields($short = 0) {
		$r = ($this->xml2array) ? $this->xml2array : $this->xml2array ();
		
		$out = array ();
		if (count ( $r ['form'] )) {
			$r = $r ['form'];
			
			if (count ( $r ['field'] )) {
				foreach ( $r ['field'] as $field ) {
					if ( ( ! $short || ($short && $field ['inShort']) ) && $field['name'] !== 'title' )
						$out [] = array ('name' => $field ['name'], 'caption' => iconv ( 'utf-8', 'windows-1251', $field ['caption'] ), 'required' => $field ['required'] );
				}
			}
		}
		return $out;
	}
}

/*
$p = new parser( 'fields.xml' );
$out = $p->getRequired();

print_r( $out );*/

